

class DoubleArrow extends AreaPlotBase {

  constructor(points) {
    super(points);

    this.connPoint;
    this.tempPoint4;

    this.headHeightFactor = 0.2;
    this.headWidthFactor = 0.4;
    this.neckHeightFactor = 0.75;
    this.neckWidthFactor = 0.15;

  }


  get_minPointCount() {
    return 2;
  }

  create() {
    let n = this.points.length;
    if (n >= 3 && this.points[n - 1] != this.points[n - 2]) {
      let pnt1 = points[0];
      let pnt2 = points[1];
      let pnt3 = points[2];

      if (n == 3)
        this.tempPoint4 = this.getTempPnt4(pnt1, pnt2, pnt3);
      else
        this.tempPoint4 = points[3];

      if (this.plotState == DRAWING)
      //this.connPoint=this.getConnPnt(this.points[0], this.points[1], this.points[2]);
        this.connPoint = super.getMidPoint(pnt1, pnt2);

      let leftArrowPnts = this.getArrowPoints(pnt1, this.connPoint, this.tempPoint4, LEFT_SIDE);
      let rightArrowPnts = this.getArrowPoints(this.connPoint, pnt2, pnt3, RIGHT_SIDE);

      let m = leftArrowPnts.Count;
      let t = (m - 5) / 2;

      let llBodyPnts = leftArrowPnts.slice(0, t);
      let lArrowPnts = leftArrowPnts.slice(t, 5 + t);
      let lrBodyPnts = leftArrowPnts.slice(t + 5, m);

      let rlBodyPnts = rightArrowPnts.slice(0, t);
      let rArrowPnts = rightArrowPnts.slice(t, 5 + t);
      let rrBodyPnts = rightArrowPnts.slice(t + 5, m);

      llBodyPnts = super.getBezierPoints(llBodyPnts);
      lrBodyPnts.concat(rlBodyPnts);
      let bodyPnts = super.getBezierPoints(lrBodyPnts);
      rrBodyPnts = super.getBezierPoints(rrBodyPnts);


      llBodyPnts = llBodyPnts.concat(lArrowPnts).concat(bodyPnts).concat(rArrowPnts).concat(rrBodyPnts);
      // llBodyPnts.concat(bodyPnts);
      // llBodyPnts.concat(rArrowPnts);
      // llBodyPnts.concat(rrBodyPnts);

      let pc3 = [];
      for (let j = 0; j < llBodyPnts.length; j++) {
        pc3.push(llBodyPnts[j]);
      }
      if (llBodyPnts.Count > 0) {
        pc3.push(llBodyPnts[0]);
      }
      if (this.PartCount > 0) {
        this.RemovePart(0);
      }
      this.pushPart(pc3);
    }
  }

  getArrowPoints(pnt1, pnt2, pnt3, side) {
    let midPnt = super.getMidPoint(pnt1, pnt2);
    let len = super.distance(midPnt, pnt3);
    let midPnt1 = super.getThirdPoint(pnt3, midPnt, 0, len * 0.3, LEFT_SIDE);
    let midPnt2 = super.getThirdPoint(pnt3, midPnt, 0, len * 0.5, LEFT_SIDE);
    let midPnt3 = super.getThirdPoint(pnt3, midPnt, 0, len * 0.7, LEFT_SIDE);
    midPnt1 = super.getThirdPoint(midPnt, midPnt1, Math.PI * 1.5, len / 4, side);
    midPnt2 = super.getThirdPoint(midPnt, midPnt2, Math.PI * 1.5, len / 4, side);
    midPnt3 = super.getThirdPoint(midPnt, midPnt3, Math.PI * 1.5, len / 4, side);

    let points = [];
    points.push(midPnt);
    points.push(midPnt1);
    points.push(midPnt2);
    points.push(midPnt3);
    points.push(pnt3);

    // 计算箭头部分
    let arrowPnts = super.getArrowHeadPoints(points, this.headHeightFactor, this.headWidthFactor, this.neckHeightFactor, this.neckWidthFactor);
    let neckLeftPoint = arrowPnts[0];
    let neckRightPoint = arrowPnts[4];
    // 计算箭身部分
    let tailWidthFactor = super.distance(pnt1, pnt2) / super.getBaseLength(points) / 2;
    let leftFactor = (side == LEFT_SIDE ? 1 : 0.01);
    let rightFactor = (side == LEFT_SIDE ? 0.01 : 1);
    let bodyPnts = super.getArrowBodyPoints(points, neckLeftPoint, neckRightPoint, tailWidthFactor, leftFactor, rightFactor);
    let n = bodyPnts.length;
    let lPoints = bodyPnts.slice(0, n / 2);
    let rPoints = bodyPnts.slice(n / 2, n );
    // 合并箭颈点
    lPoints.push(neckLeftPoint);
    rPoints.push(neckRightPoint);
    // 合并箭尾点
    lPoints.reverse();
    lPoints.push(pnt1);
    rPoints.reverse();
    rPoints.push(pnt2);

    lPoints.reverse();
    lPoints = lPoints.concat(arrowPnts);
    lPoints = lPoints.concat(rPoints);
    return lPoints;
  }

  getControlPoints() {
    let controlPnts = [];
    controlPnts = controlPnts.concat(this.points);
    if (controlPnts.length == 3)
      controlPnts.push(this.tempPoint4);
    controlPnts.push(this.connPoint);
    return controlPnts;
  }

  updateControlPoint(point, index) {
    if (index == 4)
      this.connPoint = point;
    base.updateControlPoint(point, index);
  }

  finishDraw() {
    if (this.get_pointCount() == 3)
      this.points.push(this.tempPoint4);
    super.finishDraw();
  }

  setControlPoints(value) {
    this.points = value.slice(0, this.get_pointCount());
    this.connPoint = value[value.length - 1];
    this.create();
  }

// 获得两个箭标的连接点
  getConnPnt(pnt1, pnt2, pnt3) {
    let mid = super.getMidPoint(pnt1, pnt2);
    let len = super.distance(pnt2, pnt3);
    return super.getThirdPoint(pnt1, mid, Math.PI / 2, len / 20, super.getSide(pnt1, pnt2, pnt3));
  }

// 第三个点的对称点
  getTempPnt4(linePnt1, linePnt2, point) {
    let symPnt;
    let midPnt = super.getMidPoint(linePnt1, linePnt2);
    let len = super.distance(midPnt, point);
    let angle = super.getAngleOfThreePoints(linePnt1, midPnt, point);
    let distance1 = 0;
    let distance2 = 0;
    let mid;
    if (angle < Math.PI / 2) {
      distance1 = len * Math.sin(angle);
      distance2 = len * Math.cos(angle);
      mid = super.getThirdPoint(linePnt1, midPnt, Math.PI * 1.5, distance1, LEFT_SIDE);
      symPnt = super.getThirdPoint(midPnt, mid, Math.PI * 1.5, distance2, RIGHT_SIDE);
    }
    else if (angle >= Math.PI / 2 && angle < Math.PI) {
      distance1 = len * Math.sin(Math.PI - angle);
      distance2 = len * Math.cos(Math.PI - angle);
      mid = super.getThirdPoint(linePnt1, midPnt, Math.PI * 1.5, distance1, LEFT_SIDE);
      symPnt = super.getThirdPoint(midPnt, mid, Math.PI * 1.5, distance2, LEFT_SIDE);
    }
    else if (angle >= Math.PI && angle < Math.PI * 1.5) {
      distance1 = len * Math.sin(angle - Math.PI);
      distance2 = len * Math.cos(angle - Math.PI);
      mid = super.getThirdPoint(linePnt1, midPnt, Math.PI * 1.5, distance1, RIGHT_SIDE);
      symPnt = super.getThirdPoint(midPnt, mid, Math.PI * 1.5, distance2, RIGHT_SIDE);
    }
    else {
      distance1 = len * Math.sin(Math.PI * 2 - angle);
      distance2 = len * Math.cos(Math.PI * 2 - angle);
      mid = super.getThirdPoint(linePnt1, midPnt, Math.PI * 1.5, distance1, RIGHT_SIDE);
      symPnt = super.getThirdPoint(midPnt, mid, Math.PI * 1.5, distance2, LEFT_SIDE);
    }
    return symPnt;
  }
}